<?php
session_cache_expire(999999999);
if (!isset($_SESSION)) {
    session_start();
}

$lang = $_SESSION['lang'];
if (empty($lang) || $lang == "EN") {
    include "../../value/value_en.php";
} else if ($lang == "VN") {
    include "../../value/value_vn.php";
} else {
    include "../../value/value_kr.php";
}
include '../../operations/connection.php';
include '../../content-accountant/invoicetax.php';
$tonggiacungcap = 0;
$tongthue = 0;
$tongtien = 0;
$loi = '';
$status = $_REQUEST['status'];
if ($status == 0) {
    $l_ngaygiaodich = $l_ngaygiaodich0;
    $l_ngayphathanh = $l_ngayphathanh0;
    $l_thoigiangiaodich = $l_thoigiangiaodich0;
}
if ($status == 1) {
    $l_ngaygiaodich = $l_ngaygiaodich1;
    $l_ngayphathanh = $l_ngayphathanh1;
    $l_thoigiangiaodich = $l_thoigiangiaodich1;
}
$_SESSION['invoice_arr'] = array();
if (isset($_REQUEST['type'])) {
    if ($_REQUEST['type'] == "del") {
        if (isset($_REQUEST['id'])) {
            try {
                $sql = "delete from or_order_tax where or_order_tax.orderid=" . $_REQUEST['id'];
                mysql_query($sql);
                $loi = "l_xoathanhcong";
            } catch (exception $e) {
                $loi = $e->getMessage();
            }
        }
    } else if ($_REQUEST['type'] == "up") {
        try {
            $sl = count($_SESSION['input_invoice']);
            if ($sl > 0) {
                for ($i = 0; $i < $sl; $i++) {
                    $inobject = unserialize(serialize($_SESSION['input_invoice'][$i]));

                    if ($inobject->order_time_transaction == "-")
                        $nstatus = 1;
                    else
                        $nstatus=0;
	
	                if ($inobject->order_time_transaction == "")
	                    $date = "'null'";
	                else
	                    $date="'" . $inobject->order_time_transaction . "'";
	                    
	                if ($inobject->order_time_payment == "")
	                    $date1 = "'null'";
	                else
	                    $date1="'" . $inobject->order_time_payment . "'";
	                 
                    $vdate = $inobject->oryear . "-" . $inobject->ormonth . "-" . $inobject->orday;
                    
                    if($inobject->cusname!="" && $inobject->order_content!=""){   
                    $sql = "UPDATE or_order_tax set or_order_tax.order_time='" . $vdate . "',
					or_order_tax.order_cusname='" . $inobject->cusname . "',
					or_order_tax.order_content='" . $inobject->order_content . "',
					or_order_tax.order_quantity=" . $inobject->order_quantity . ",
					or_order_tax.order_supply_rice=" . $inobject->order_suplly_rice . ",
					or_order_tax.order_tax=" . $inobject->order_tax . ",
					or_order_tax.order_total=" . $inobject->order_total . ",
					or_order_tax.user_create='" . $_SESSION['IDEmployee'] . "',
					or_order_tax.date_create=now(),
					or_order_tax.order_time_transaction=" . $date . ",
					or_order_tax.order_time_payment=".$date1. ", 
					or_order_tax.order_memo='" . $inobject->order_memo . "',
					or_order_tax.status='" . $nstatus . "',
					or_order_tax.red='" . $inobject->red . "'
					where or_order_tax.orderid=" . $inobject->orderid;

                    mysql_query($sql);
                    }
                }
                $loi = 'l_capnhatthanhcong';
            }
        } catch (exception $e) {
            $loi = $e->getMessage();
        }
    }
} else {
    if (isset($_SESSION['input_invoice'])) {
        $sl = count($_SESSION['input_invoice']);
        if ($sl > 0) {
            $sql = "INSERT into or_order_tax (order_time,order_cusname,order_content,order_quantity,
											order_supply_rice,order_tax,order_total,order_time_transaction,order_time_payment,
											order_memo,user_create,date_create,is_status,status,red) values ";
            $tem = "";
            //echo $sl;
            for ($i = 0; $i < $sl; $i++) {

                $inobject = unserialize(serialize($_SESSION['input_invoice'][$i]));

                if ($inobject->order_time_transaction == "-")
                    $nstatus = 1;
                else
                    $nstatus=0;
				$red=$inobject->red;
                if ($inobject->order_time_transaction == "")
                    $date = "'null'";
                else
                    $date="'" . $inobject->order_time_transaction . "'";
                    
                if ($inobject->order_time_payment == "")
                    $date1 = "'null'";
                else
                    $date1="'" . $inobject->order_time_payment . "'";

                $order_time = $inobject->oryear . "-" . $inobject->ormonth . "-" . $inobject->orday;
				if($inobject->cusname!="" && $inobject->order_content!="")
                if ($tem == "") {
                    $tem = "('" . $order_time . "','" . $inobject->cusname . "','" . $inobject->order_content . "'," . $inobject->order_quantity . "," . $inobject->order_suplly_rice . "," . $inobject->order_tax . "," . $inobject->order_total . "," . $date . ",".$date1. ",'" . $inobject->order_memo . "','" . $_SESSION['IDEmployee'] . "',now()," . $status . "," . $nstatus . "," . $red . ")";
                } else {
                    $tem .=",('" . $order_time . "','" . $inobject->cusname . "','" . $inobject->order_content . "'," . $inobject->order_quantity . "," . $inobject->order_suplly_rice . "," . $inobject->order_tax . "," . $inobject->order_total . ",". $date . ",".$date1. ",'" . $inobject->order_memo . "','" . $_SESSION['IDEmployee'] . "',now()," . $status . "," . $nstatus . "," . $red . ")";
                }
            }
        }
        if ($tem != "") {
            try {
                //echo $sql.$tem;
                mysql_query($sql . $tem);
                //echo $sql.$tem;
                $loi = "l_themthanhcong";
            } catch (exception $e) {
                $loi = $e->getMessage();
            }
        } else {
            $loi = $l_khongluu;
        }
    }
}


$sqlinvoice = mysql_query("SELECT or_order_tax.* from or_order_tax where month(or_order_tax.order_time)='" . $_REQUEST['thang'] . "' and year(or_order_tax.order_time)='" . $_REQUEST['nam'] . "' and is_status='" . $status . "' ORDER BY or_order_tax.order_time,or_order_tax.order_time_transaction,or_order_tax.order_cusname asc");
?>
<table cellpadding="0" cellspacing="1" width="100%" border="0" class="ftable ">
    <tr >
        <th style="width:10px"></th>
        <th style="width:10px"><?= $l_no ?></th>
        <th style="width:30px"><?= $l_month ?></th>
        <th style="width:30px"><?= $l_days ?></th>
        <th style="width:220px"><?= $l_customer ?></th>
        <th style="width:30%"><?= $l_product_item ?></th>
        <th style="width:50px;display: none;"><?= $l_quantity ?></th>
        <th style="width:90px"><?= $l_Supply_price ?></th>
        <th style="width:90px"><?= $l_vat ?></th>
        <th style="width:90px"><?= $l_amount ?></th>
        <th style="width:90px"><?= $l_thoigiangiaodich ?></th> 
        <th style="width:90px;<?php if($status==0) echo 'display: none'?>"><?= $l_payment ?></th> 
        <th style="width:10%"><?= $l_memo ?></th>
    </tr>
    <?php
    if (mysql_num_rows($sqlinvoice) > 0) {
        $soluonggiaodich = 0;
        $tonggiacungcap = 0;
        $tongthue = 0;
        $tongtien = 0;
        $soluonggiaodich1 = 0;
        $tonggiacungcap1 = 0;
        $tongthue1 = 0;
        $tongtien1 = 0;

        $vitri = 0;
        $temp = "";
        $i = 0;
        while ($rin = mysql_fetch_array($sqlinvoice)) {
            $inobject = new invoicetax();
            $ktdate = getdate(strtotime($rin[order_time]));
            $inobject->Instance($rin['orderid'], $rin['order_time'], $ktdate['mon'], $ktdate['mday'], $ktdate['year'], $rin['order_cus'], $rin['order_content'], 
                        					$rin['order_quantity'], $rin['order_supply_rice'], $rin['order_tax'], $rin['order_total'], $rin['order_time_transaction'], 
                        					$rin['is_status'], $rin['user_create'], $rin['date_create'], $rin['order_memo'], $rin['status']);
                        $inobject->setCusname($rin['order_cusname']);
                        $inobject->setPayment($rin['order_time_payment']);
						$inobject->setRED($rin['red']);

            
            ?>
            <tr style="padding:3px;<?php if($status==1){if($inobject->red==1) echo 'background: #FF9999;';if($inobject->red==0) echo 'background: #d0d8e8;';}?>" >

                <td>
                    <img onclick="var hoi=confirm('<?=$l_confirm_deleteproject?>'); if(hoi==true){delOneRow(<?= $inobject->orderid ?>,<?= $status ?>);}" src="images/del.png" style="width:20px;height:20px;cursor:pointer;" title="<?php echo $l_delete; ?>" />
                </td>
                <td style="padding: 5px;cursor:pointer;" onclick="editOneRow(<?= $vitri ?>,<?= $inobject->orderid ?>,<?= $status ?>)"><p style="width: 20px"><?= ($vitri + 1) ?></p></td>
                <td style="cursor:pointer;" onclick="editOneRow(<?= $vitri ?>,<?= $inobject->orderid ?>,<?= $status ?>)"><p style="width: 20px"><?= $inobject->ormonth ?></p></td>
                <td style="cursor:pointer;" onclick="editOneRow(<?= $vitri ?>,<?= $inobject->orderid ?>,<?= $status ?>)"><p style="width: 20px"><?= $inobject->orday ?></p></td>
                <td style="cursor:pointer;text-align:left;padding-left:3px;" onclick="editOneRow(<?= $vitri ?>,<?= $inobject->orderid ?>,<?= $status ?>)"><p style="width: 200px"><?php echo $inobject->cusname; ?></p></td>
                <td style="cursor:pointer;text-align:left;padding-left:3px;" onclick="editOneRow(<?= $vitri ?>,<?= $inobject->orderid ?>,<?= $status ?>)"><p style="min-width: 200px"><?= $inobject->order_content ?></p></td>
                <td style="display: none;cursor:pointer;" onclick="editOneRow(<?= $vitri ?>,<?= $inobject->orderid ?>,<?= $status ?>)"><p style="width: 30px"><?= number_format($inobject->order_quantity) ?></p></td>
                <td style="cursor:pointer;text-align:right;padding-right:3px;" onclick="editOneRow(<?= $vitri ?>,<?= $inobject->orderid ?>,<?= $status ?>)"><p style="width: 90px"><?= number_format($inobject->order_suplly_rice) ?></p></td>
                <td style="cursor:pointer;text-align:right;padding-right:3px;" onclick="editOneRow(<?= $vitri ?>,<?= $inobject->orderid ?>,<?= $status ?>)"><p style="width: 90px"><?= number_format($inobject->order_tax) ?></p></td>
                <td style="cursor:pointer;text-align:right;padding-right:3px;" onclick="editOneRow(<?= $vitri ?>,<?= $inobject->orderid ?>,<?= $status ?>)"><p style="width: 90px"><?= number_format($inobject->order_total) ?></p></td>
                <td style="cursor:pointer;" onclick="editOneRow(<?= $vitri ?>,<?= $inobject->orderid ?>,<?= $status ?>)">
                    <p style="width: 80px"> <?php
        if ($inobject->nstatus == 1)
            echo "-";
        else
            echo $inobject->order_time_transaction;
            ?>
                    </p>
                </td>
                <td style="cursor:pointer;<?php if($status==0) echo 'display: none'?>;" onclick="editOneRow(<?= $vitri ?>,<?= $inobject->orderid ?>,<?= $status ?>)">
                    <p style="width: 80px"> <?php echo $inobject->order_time_payment;?></p>
                </td>
                <td style="cursor:pointer;" onclick="editOneRow(<?= $vitri ?>,<?= $inobject->orderid ?>,<?= $status ?>)"><p style="min-width: 120px"><?= $inobject->order_memo ?></p></td>

                <?php
                $vitri++;
                $soluonggiaodich++;
                $tonggiacungcap +=$inobject->order_suplly_rice;
                $tongthue +=$inobject->order_tax;
                $tongtien +=$inobject->order_total;

               
                ?></tr><?php
        $_SESSION['invoice_arr'][$rin[orderid]] = $inobject;
    }
            ?>
       
        <?php
    }else {
        ?>
        <tr><td colspan="12" style="height:30px;text-align:left;padding-left:20px;"><?php echo $l_list_emplty; ?></td></tr>
    <?php } ?>
    <tr class="coltr2" style="height:30px;font-weight:bold;">
        <td colspan="5"><?php echo $l_all . '-' . $l_month; ?> : <font color="red"><?php echo $_SESSION['months'] ?></font>  -  <?php echo $l_co; ?>  <font color="blue"><?php echo number_format($soluonggiaodich); ?></font> <?php echo $l_giaodich; ?></td>
        <td></td>
        <?php if($status==1){?><td></td><?php }?>
        <td style="display: none;"></td>
        <td style="text-align:right;padding-right:3px;"><?php echo number_format($tonggiacungcap); ?></td>
        <td style="text-align:right;padding-right:3px;"><?php echo number_format($tongthue); ?></td>
        <td style="text-align:right;padding-right:3px;"><?php echo number_format($tongtien); ?></td>
        <td colspan="3"></td>
    </tr>            
</table>
<input type="hidden" value="<?= $loi ?>" id="convoi" /> 
<?php mysql_close($connection); ?>